package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRoleListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取角色列表
func NewGetRoleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRoleListLogic {
	return &GetRoleListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetRoleListLogic) GetRoleList(req *types.GetRoleListRequest) (resp *types.GetRoleListResponse, err error) {
	reply, err := l.svcCtx.AuthRoleRpc.GetRoleList(l.ctx, &v1.GetRoleListRequest{
		Page:     req.Page,
		PageSize: req.PageSize,
		RolePid:  req.RolePid,
		RoleName: req.RoleName,
		Status:   v1.Status(req.Status),
	})
	if err != nil {
		return resp, err
	}

	resp = &types.GetRoleListResponse{ResponsePagination: types.ResponsePagination{
		Page:      reply.GetPagination().GetPage(),
		PageSize:  reply.GetPagination().GetPageSize(),
		TotalPage: reply.GetPagination().GetTotalPage(),
		Total:     reply.GetPagination().GetTotal(),
	}}

	for _, role := range reply.List {
		resp.List = append(resp.List, types.RoleList{
			Id:          role.GetId(),
			RolePid:     role.GetRolePid(),
			RoleName:    role.GetRoleName(),
			Description: role.GetDescription(),
			Remark:      role.GetRemark(),
			ListOrder:   role.GetListOrder(),
			Status:      int8(role.GetStatus()),
			CreatedAt:   role.GetCreatedAt(),
			UpdatedAt:   role.GetUpdatedAt(),
		})
	}

	return resp, err
}
